package com.tqyan.service.service.impl;

import com.tqyan.dal.mapper.AccountMapper;
import com.tqyan.dal.mapper.BillMapper;
import com.tqyan.dal.mapper.UserMapper;
import com.tqyan.model.dto.AccountDTO;
import com.tqyan.model.dto.BillDTO;
import com.tqyan.model.enums.BillTypeEnum;
import com.tqyan.model.exp.DefaultException;
import com.tqyan.service.service.AccountService;
import com.tqyan.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 账户服务
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/4/14
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BillMapper billMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AccountDTO queryByUser(long userId) {
        AccountDTO account = accountMapper.queryByUser(userId);
        if (null != account) {
            return account;
        }
        // 新增用户账户
        account = new AccountDTO();
        account.setUserId(userId);
        account.setAccount(0L);
        boolean result = accountMapper.save(List.of(account)) > 0;
        if (!result) {
            throw new DefaultException("用户账户信息入库失败");
        }
//        // 赠送余额, 10元
//        account.setAccount(10 * 100L);
//        this.gift(userId, account.getAccount(), "新用户注册");
        return account;
    }

    @Override
    public boolean isArrears(long userId, long min) {
        return false;
//        if (min <= 0) {
//            return false;
//        }
//        AccountDTO account = this.queryByUser(userId);
//        if (null == account) {
//            throw new DefaultException("用户账户信息不存在");
//        }
//        return account.getAccount() <= min;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean gift(long userId, long account, String explain) {
        return true;
//        if (account <= 0) {
//            return true;
//        }
//        // 添加余额
//        boolean result = accountMapper.add(userId, account) > 0;
//        if (!result) {
//            log.error("[赠送] userId={}, account={}, explain={}, 余额赠送失败"
//                    , userId, account, explain);
//            return true;
//        }
//        // 记录账单
//        BillDTO bill = new BillDTO();
//        bill.setUserId(userId);
//        bill.setAccount(account);
//        bill.setType(BillTypeEnum.GIFT.getValue());
//        // 赠送余额有效期3个月
//        bill.setExpireTime(DateUtil.addMonth(DateUtil.now(), 3));
//        // 账单说明
//        bill.setExplain(explain);
//        if (StringUtils.isBlank(explain)) {
//            bill.setExplain("赠送");
//        }
//        result = billMapper.save(bill) > 0;
//        if (!result) {
//            throw new DefaultException("账单入库失败");
//        }
//        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean recharge(long userId, long account, String explain) {
        if (account <= 0) {
            return true;
        }
        // 添加余额
        boolean result = accountMapper.add(userId, account) > 0;
        if (!result) {
            throw new DefaultException("余额充值失败");
        }
        // 记录账单
        BillDTO bill = new BillDTO();
        bill.setUserId(userId);
        bill.setAccount(account);
        bill.setType(BillTypeEnum.RECHARGE.getValue());
        // 账单说明
        bill.setExplain(explain);
        if (StringUtils.isBlank(explain)) {
            bill.setExplain("充值");
        }
        result = billMapper.save(bill) > 0;
        if (!result) {
            throw new DefaultException("账单入库失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean consumption(long userId, long account, String explain) {
        return true;
//        if (account <= 0) {
//            return true;
//        }
//        // 扣减余额
//        boolean result = accountMapper.sub(userId, account) > 0;
//        if (!result) {
//            throw new DefaultException("余额消费失败");
//        }
//        // 记录账单
//        BillDTO bill = new BillDTO();
//        bill.setUserId(userId);
//        bill.setAccount(account);
//        bill.setType(BillTypeEnum.CONSUMPTION.getValue());
//        // 账单说明
//        bill.setExplain(explain);
//        if (StringUtils.isBlank(explain)) {
//            bill.setExplain("消费");
//        }
//        result = billMapper.save(bill) > 0;
//        if (!result) {
//            throw new DefaultException("账单入库失败");
//        }
//        return true;
    }

}


